import Vue, {nextTick} from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from "@/store";
import it from "element-ui/src/locale/lang/it";
Vue.use(VueRouter);

const routes = [

    {
  path: '/',
  name: 'Manage',
      component: ()=> import('../views/Manage'),
      redirect:"/home",
      children:[
        {path:'home',name:'首页',component:()=>import('../views/Home.vue')},
        {path:'user',name:'用户管理',component:()=>import('../views/User.vue')},
        {path:'person',name:'个人管理',component:()=>import('../views/Person.vue')},
        {path:'file',name:'文件管理',component:()=>import('../views/File.vue')},
        {path:'role',name:'角色管理',component:()=>import('../views/Role.vue')},
        {path:'menu',name:'菜单管理',component:()=>import('../views/Menu.vue')},
        {path:'baidu',name:'百度翻译',component:()=>import('../views/TestBaidu.vue')},
        {path:'Zjhhome',name:'计划',component:()=>import('../views/zhoujihua/ZjhHome.vue')},
        {path:'ZjhContent',name:'具体内容',component:()=>import('../views/zhoujihua/ZjhContent.vue')},
        {path:'ZjhPage',name:'自定义是否完成',component:()=>import('../views/zhoujihua/ZjhPage.vue')},
        {path:'ZjhPagetotal',name:'自定义每日进度',component:()=>import('../views/zhoujihua/ZjhPagetotal.vue')},
        {path:'ZjhPagetask', name:'自定义配置', component:()=>import('../views/zhoujihua/ZjhPagetask.vue')},
      ]

},
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path:'/login',
    name:'/Login',
    component:()=>import('../views/Login.vue')
  }
  ,
  {
    path:'/register',
    name:'/Register',
    component:()=>import('../views/Register')
  }
  ,
  {
    path:'*',
    name:'/error',
    component:()=>import('../views/error.vue')
  }
];
// setRoutes()
// 刷新页面会导致页面路由重置
// export const setRoutes = () =>{
//   const storeMenus  = localStorage.getItem("menus");
//   if (storeMenus){
//     // 拼装动态路由
//     const manageRoute ={path:'/',name:"Manage",component:()=>import('../views/Manage.vue'),redirect:"/home",children:[]};
//     const menus = JSON.parse(storeMenus);
//     menus.forEach(item => {
//       // path:item.path.replace("/","") 去掉 ‘/’
//       if (item.path){// 当path有值的时候才会设置
//         let itemMenu = {path:item.path.replace("/",""),name:item.name,component:()=>import('../views/'+item.pagePath+'.vue')};
//         manageRoute.children.push(itemMenu)
//       }else if (item.children.length){
//         item.children.forEach(item=>{
//             let itemMenu ={path:item.path.replace("/",""),name:item.name,component:()=>import('../views/'+item.pagePath+'.vue')};
//             manageRoute.children.push(itemMenu)
//             }
//         )
//       }
//     });
//     // 获取当前路由名称
//     const currentRouteNames = router.getRoutes().map(o=>o.name);
//     if(!currentRouteNames.includes("Manage"))
//     // 返回
//     router.addRoute(manageRoute)
//   }
// };
//
// setRoutes();
// setRoutes()
// 动态加载路由
// router.addRoute(
//     setRoutes()

//     )
// 重置路由的方法
// export const resetRouter=()=>{
//   const router = new VueRouter({
//     mode: 'history',
//     base: process.env.BASE_URL,
//     routes,
//   });
// }
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});



// export const setRoutes = () =>{
//   const storeMenus  = localStorage.getItem("menus");
//   const manageRoute={}
//   if (storeMenus){
//     // 获取当前路由名称
//     const currentRouteNames = router.getRoutes().map(o=>o.name);
//     if(!currentRouteNames.includes("Manage")){
//       // 拼装动态路由
//       const manageRoute ={path:'/',name:"Manage",component:()=>import('../views/Manage.vue')
//         ,redirect:"/home",children:[{path:'person',name:'个人管理',component:()=>import('../views/Person.vue')},]};
//       const menus = JSON.parse(storeMenus);
//       menus.forEach(item => {
//         // path:item.path.replace("/","") 去掉 ‘/’
//         if (item.path){// 当path有值的时候才会设置
//           let itemMenu = {path:item.path.replace("/",""),name:item.name,component:()=>import('../views/'+item.pagePath+'.vue')};
//           manageRoute.children.push(itemMenu)
//         }else if (item.children.length){
//           item.children.forEach(item=>{
//                 let itemMenu ={path:item.path.replace("/",""),name:item.name,component:()=>import('../views/'+item.pagePath+'.vue')};
//                 manageRoute.children.push(itemMenu)
//               }
//           )
//         }
//       });
//       // 返回
//       router.addRoute(manageRoute)
//     }

//   }
// };

// setRoutes()
// 路由守卫
router.beforeEach((to,from,next)=>{
  localStorage.setItem("currentPathName",to.name);
  store.commit("setPath");

  if (!to.matched.length){
    const storeMenus = localStorage.getItem("menus")?localStorage.getItem("menus"):"";

    if (storeMenus){
      next("/404")
    }else{
      next("/login")
    }
  }
  next()

});

export default router
